<?php
/**
 * IMPORTANT: Do not use this controller for writing your own code!!
 * @return 
 */
class Newsletter extends Controller {

	function Import()
	{ 
	 parent::Controller(); 
	$this->load->model('model_import'); 
 	}
  
  
function import_file($data,$rows) {

     $esteCeva=false;
                                 
     if ($data!='')
     foreach ($data as $key=>$element)
       {
        if (trim($element) != '')  $esteCeva=true;        
       }
         
	      
      if($esteCeva){
       $dataPerson["lname"]	=mb_convert_case($data["1"], MB_CASE_TITLE, "UTF-8");
       $dataPerson["fname"]=mb_convert_case($data["2"], MB_CASE_TITLE, "UTF-8");       
       $dataPerson["email"]=explode(";",$data["4"]);
     	
	
	
	
		
	foreach($dataPerson["email"] as $email){
		if($email!=''){
			$this->db->flush_cache();
			$temp=array("lname"=>$dataPerson["lname"],"fname"=>$dataPerson["fname"],"email"=>trim($email));	
			$query=$this->db->insert("direct5_1",$temp);
		 }	
	}       
       return true;
      }
      else return false;
}   
  
  
  /*
     function import_file($data,$rows) {

     $esteCeva=false;
                                 
     if ($data!='')
     foreach ($data as $key=>$element)
       {
        if (trim($element) != '')  $esteCeva=true;        
       }
         
	      
      if($esteCeva){
	
	
	$domain_name=strTitle($data[1]);
	$sub_domain_name=strTitle($data[2]);
	
	if($domain_name!="")
	{
		
		$temp=array("name"=>"_".$domain_name,"level"=>"0","created_by"=>$this->session->userdata("id"));
		$this->db->flush_cache();
		$this->db->insert("domain",$temp);
		
		$domain_id=$this->db->insert_id();
		$this->db->flush_cache();
		$this->db->where("domain_id",$domain_id);
		$this->db->set("parent_id",$domain_id);
		$this->db->update("domain");
			
		$this->session->set_userdata("domain_id",$domain_id);			
	}
	if($sub_domain_name!="")
	{
		
		$temp=array("name"=>"_".$sub_domain_name,"level"=>"1","created_by"=>$this->session->userdata("id"));
		$this->db->flush_cache();
		$this->db->insert("domain",$temp);
		
		$sub_domain_id=$this->db->insert_id();
		$this->db->flush_cache();
		$this->db->where("domain_id",$sub_domain_id);
		$this->db->set("parent_id",$this->session->userdata("domain_id"));
		$this->db->update("domain");		
	}
	
     
       return true;
      }
      else return false;
}   
  
  */
  
  
   function importData()
     {           
       $this->load->library('Spreadsheet_Excel_Reader',"reader");
       $field_name = "file"; 
      
	$config['upload_path']= $this->config->item('upload_prov');
        $config['allowed_types'] = '*';		
        $config['remove_spaces']  =TRUE;
        $config['overwrite'] = FALSE;   
        $this->upload->initialize($config); 
        
        if (!$this->upload->do_upload($field_name)) {
		echo $this->upload->display_errors();
		//$aResponse = array ("error"=>TRUE,"description"=>"Probleme la upload","success"=>FALSE      );   
               //echo json_encode($aResponse);
        }
        else
        {
                $data = $this->upload->data();
                $sFileName=$data['file_name'];
		$data = new Spreadsheet_Excel_Reader(); 
		$data->setUTFEncoder('iconv');
		$data->setOutputEncoding('UTF-8');
		$data->read($config['upload_path'].$sFileName);  
		$rows=$data->sheets[0]['numCols'];
		
		
		if ($rows=='') $rows=0; 
		for ($j = 1; $j <= $data->sheets[0]['numRows']; $j++)
		{
		    if ($j>1){			
			$result=$this->import_file($data->sheets[0]['cells'][$j],$rows); 		    
		    }
		}
		
		
		if($result)		
		$aResponse = array ("error"=>false,"description"=>"Datele au fost salvate","success"=>true);
		else
		$aResponse = array ("error"=>true,"description"=>"Datele  NU au fost salvate","success"=>false);
		
		echo json_encode($aResponse);	
	}   
     }  
      
  
  
}

